package com.xzavier.yunlian_interview.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzavier.yunlian_interview.entity.User;
import com.xzavier.yunlian_interview.mapper.UserMapper;
import com.xzavier.yunlian_interview.service.UserService;
import com.xzavier.yunlian_interview.service.ex.LoginException;
import com.xzavier.yunlian_interview.service.ex.RegisterException;
import com.xzavier.yunlian_interview.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Integer register(String username, String password) {
        if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
            throw new RegisterException("用户账户或者密码错误");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        if (userMapper.exists(queryWrapper)) {
            throw new RegisterException("用户名被占用");
        }
        User user = new User();
        user.setUsername(username);
        user.setSalt(UUID.randomUUID().toString().toUpperCase());
        user.setPassword(getMd5Password(password, user.getSalt()));
        int insert = userMapper.insert(user);
        if (insert != 1) {
            throw new RuntimeException("注册失败");
        }
        return insert;
    }

    @Override
    public User login(String username, String password) {
        if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
            throw new LoginException("参数错误");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.isEmpty()) {
            throw new LoginException("用户未注册");
        }
        User user = users.getFirst();
        String password1 = user.getPassword();
        if (!getMd5Password(password, user.getSalt()).equals(password1)) {
            throw new LoginException("用户密码错误");
        }
        user.setToken(JwtUtils.GetToken(user));
        userMapper.updateById(user);
        return user;
    }

    @Override
    public boolean updateUserInfo(User user) {
        if (user == null) {
            throw new LoginException("参数缺失");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", user.getId());
        if (!userMapper.exists(queryWrapper)) {
            throw new LoginException("用户不存在");
        }
        int i = userMapper.updateById(user);
        return i == 1;
    }

    @Override
    public boolean updateAvatar(String userid, byte[] file) throws SQLException {
        if (file == null) {
            throw new RegisterException("文件不能为空");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userid);
        if (!userMapper.exists(queryWrapper)) {
            throw new RegisterException("没有该用户");
        }
        User user = userMapper.selectOne(queryWrapper);
        user.setAvatar(file);

        int i = userMapper.updateById(user);
        return i == 1;
    }

    @Override
    public byte[] getAvatar(String userid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userid);
        if (!userMapper.exists(queryWrapper)) {
            throw new RegisterException("没有该用户");
        }
        User user = userMapper.selectOne(queryWrapper);
        return user.getAvatar() != null && user.getAvatar().length > 1 ? user.getAvatar() : null;
    }

    private String getMd5Password(String password, String salt) {
        for (int i = 0; i < 3; i++) {
            //md5加密算法方式的调用
            password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toUpperCase();
        }
        //返回密码
        return password;

    }
}
